//
//  UnlockedCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/10.
//

import UIKit

class UnlockedCell: UITableViewCell {
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(2)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        return label
    }()
    
    lazy var chapterLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 12)
        label.textColor = .init(named: "gray_999999")
        return label
    }()
    
    lazy var costLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .right
        label.font = .wn_font(name: .bold, size: 13)
        label.textColor = .black
        return label
    }()
    
    lazy var ticketImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "ticket_small"))
        return image
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "gray_D0D0D0")
        return view
    }()
    
    var model: OrderDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                if m.type == 1 {
                    chapterLabel.text = "全書"
                }else {
                    chapterLabel.text = m.chapterName
                }
                timeLabel.text = m.time?.wn_toJavaDetailDate()
                costLabel.text = "-\(m.cost)"
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(bookImage)
        contentView.addSubview(nameLabel)
        contentView.addSubview(chapterLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(costLabel)
        contentView.addSubview(ticketImage)
        contentView.addSubview(lineView)
        bookImage.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
            make.size.equalTo(CGSize(width: scaleWidth(60), height: scaleWidth(88)))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.left.equalTo(bookImage.snp.right).offset(14)
            make.right.equalTo(-12)
        }
        chapterLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(1)
            make.left.equalTo(nameLabel.snp.left)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.bottom.equalTo(-12)
        }
        costLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
        }
        ticketImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(costLabel.snp.left).offset(-3)
            make.size.equalTo(CGSize(width: 16, height: 16))
        }
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.bottom.right.equalTo(0)
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
